package com.itheima.service.company.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.company.CompanyDao;
import com.itheima.domain.company.Company;
import com.itheima.service.company.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    /**
     * 查询所有企业
     *
     * @return
     */
    @Override
    public PageInfo<Company> findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Company> list = companyDao.findAll();
        return new PageInfo<Company>(list);
    }

    /**
     * 保存企业信息
     *
     * @param company
     */
    @Override
    public void save(Company company) {
        String s = UUID.randomUUID().toString();
        company.setId(s);
        companyDao.save(company);
    }

    /**
     * 修改企业信息
     *
     * @param company
     */
    @Override
    public void update(Company company) {
        companyDao.update(company);
    }

    /**
     * 根据ID查找企业
     *
     * @param id
     */
    @Override
    public Company findById(String id) {
        Company findCompany = companyDao.findById(id);
        return findCompany;
    }

    /**
     * 根据ID删除记录
     *
     * @param id
     */
    @Override
    public void deleteCompany(String id) {
        companyDao.deleteById(id);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Company> findByPage(Integer pageNum, Integer pageSize) {
        //开始分页
        PageHelper.startPage(pageNum, pageSize);//关键代码，其后第一条查询自动分页
        List<Company> list = companyDao.findAll();
        return new PageInfo<Company>(list);
    }
}
